import threading
import xmlrpclib
import difflib
from localEnv import *
class RdiffTask(threading.Thread):
    """diff file use difflib.context_diff and report to caller"""
    def __init__(self,address,port,arg=None):
        threading.Thread.__init__(self)
        self.ip=address
        self.port=port
        self.file=arg[0]
        self.str=arg[1]
        print "RdiffTask __init__ finish"


    def run(self):
        try:
            f=open(self.file,'r')
        except:
            print "open file error",self.file
        text=f.read()
        #print text
        host=get_host_name()
        d=difflib.context_diff(self.str.splitlines(1),text.splitlines(1),"Original File",host+":"+self.file)
        diff_info=''.join(d)
        print diff_info
        callback= "http://"+self.ip+":"+str(self.port)
        server=xmlrpclib.ServerProxy(callback)
        if not diff_info=='':
            server.report_diff(host,diff_info)
        else:
            server.report_same(host)


